package com.zhongwang.business.module.estimateBudgetAccountingManage.excelListener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.zhongwang.business.common.ImportCallBack;
import com.zhongwang.business.gen.entity.BusBudgetAccountingCost;
import com.zhongwang.business.gen.entity.BusBudgetAccountingTask;
import com.zhongwang.business.gen.entity.BusMaterialData;
import com.zhongwang.business.module.estimateBudgetAccountingManage.dao.AccountingInfoDao;
import com.zhongwang.business.module.estimateBudgetAccountingManage.model.excelImport.AccOutImportModel;
import com.zhongwang.business.module.estimateBudgetAccountingManage.model.excelImport.AccRawMatImportModel;
import com.zhongwang.business.util.BeanUtil;
import com.zhongwang.business.util.ShiroUtils;
import com.zhongwang.business.util.StringUtils;

import java.util.*;

/**
 * @author 徐一贺
 * @className AccOutImportListener
 * @description 外协件信息导入监听器
 * @date 2020/11/21 14:46
 * @versron ver 1.0
 */
public class AccOutImportListener extends AnalysisEventListener<AccOutImportModel> {
  /** 钩子 */
  private ImportCallBack callBack;
  /** Excel相关参数模型 */
  private BusMaterialData param;
  /** 材料信息数据持久层 */
  private AccountingInfoDao accountingInfoDao;
  /** 错误列表 */
  private List<AccOutImportModel> errorList = new ArrayList<>();
  /** 持久化列表 */
  private List<AccOutImportModel> persistenceList = new ArrayList<>();
  /** 当前时间 */
  private Date currentTime = new Date();
  /** 专业部分信息 */
  private Map<String, List<String>> majorParts;

  /**
   * 构造函数
   *
   * @param param 页面参数
   * @param callBack 回调函数
   * @param accountingInfoDao 持久层
   */
  public AccOutImportListener(
      BusMaterialData param, ImportCallBack callBack, AccountingInfoDao accountingInfoDao) {
    this.callBack = callBack;
    this.param = param;
    this.accountingInfoDao = accountingInfoDao;
    majorParts = new HashMap<>();
    BusBudgetAccountingCost findPartListParam = new BusBudgetAccountingCost();
    findPartListParam.setBatId(param.getBatId());
    findPartListParam.setMajor("机械");
    List<String> mechanicsPart = accountingInfoDao.findPartListByBatIdAndMajor(findPartListParam);
    findPartListParam.setMajor("流体");
    List<String> fluidPart = accountingInfoDao.findPartListByBatIdAndMajor(findPartListParam);
    findPartListParam.setMajor("自动化");
    List<String> automationPart = accountingInfoDao.findPartListByBatIdAndMajor(findPartListParam);
    majorParts.put("机械", mechanicsPart);
    majorParts.put("流体", fluidPart);
    majorParts.put("自动化", automationPart);
  }

  /**
   * 验证
   *
   * @param data 每行数据
   */
  @Override
  public void invoke(AccOutImportModel data, AnalysisContext context) {
    String errorInfo = "";
    boolean isRightMajor = false;
    if (data.getMajor() == null || StringUtils.trimAllBlank(data.getMajor()).equals("")) {
      errorInfo = "请填写专业！\r\n";
    } else if (data.getMajor() != null && data.getMajor().length() > 255) {
      errorInfo = "专业填写不正确，正确的是机械、流体或自动化！\r\n";
    }
    if (data.getMajor().equals("机械")
        || data.getMajor().equals("流体")
        || data.getMajor().equals("自动化")) {
      isRightMajor = true;
    } else {
      errorInfo = "专业填写不正确，正确的是机械、流体或自动化！\r\n";
    }
    if (data.getPart() == null || StringUtils.trimAllBlank(data.getPart()).equals("")) {
      errorInfo = "填写部分！\r\n";
    } else if (data.getPart() != null && data.getPart().length() > 255) {
      errorInfo = "部分不可超过255个字符！\r\n";
    } else if (data.getPart() != null && data.getMajor() != null && isRightMajor) {
      List<String> parts = majorParts.get(data.getMajor());
      if (!parts.contains(data.getPart())) {
        errorInfo = "部分填写错误，该专业下无此部分！\r\n";
      }
    }
    if (data.getMatDesc() == null || StringUtils.trimAllBlank(data.getMatDesc()).equals("")) {
      errorInfo = "请填物料描述！\r\n";
    } else if (data.getMatDesc() != null && data.getMatDesc().length() > 255) {
      errorInfo = "物料描述不可超过255个字符！\r\n";
    }
    if (data.getDiagram() == null || StringUtils.trimAllBlank(data.getDiagram()).equals("")) {
      errorInfo = "请填写图号！\r\n";
    } else if (data.getDiagram() != null && data.getDiagram().length() > 255) {
      errorInfo = "图号不可超过255个字符！\r\n";
    }
    if (data.getNum() == null) {
      errorInfo = "请填写数量！\r\n";
    }
    if (data.getUnit() == null || StringUtils.trimAllBlank(data.getUnit()).equals("")) {
      errorInfo = "请填写单位！\r\n";
    } else if (data.getUnit() != null && data.getUnit().length() > 255) {
      errorInfo = "单位不可超过255个字符！\r\n";
    }

    if (data.getPrice() == null) {
      errorInfo = "请填写单价！\r\n";
    }
    if (data.getAssistType() == null) {
      errorInfo = "请填写外协类型！\r\n";
    }else if (data.getAssistType() != null && !data.getAssistType().equals("整体外协") && !data.getAssistType().equals("带料外协") && !data.getAssistType().equals("铸件毛坯") && !data.getAssistType().equals("锻件毛坯")) {
      errorInfo = "外协类型填写错误，外协类型只能填写，整体外协、带料外协、铸件毛坯或锻件毛坯！\r\n";
    }
    if (errorInfo.length() > 0) {
      data.setErrorInfo(errorInfo);
      errorList.add(data);
    } else {
      persistenceList.add(data);
    }
  }

  /**
   * 持久化
   *
   * @param context
   */
  @Override
  public void doAfterAllAnalysed(AnalysisContext context) {
    if (errorList.size() > 0) {
      callBack.onWarning(errorList);
    } else {
      Date currentDate = new Date();
      BusBudgetAccountingTask order =
          accountingInfoDao.getBusBudgetAccountingTaskModelById(param.getBatId());
      for (int i = 0; i < persistenceList.size(); i++) {
        AccOutImportModel persistenceData = persistenceList.get(i);
        persistenceData.setBatId(param.getBatId());
        persistenceData.setDataUseType(2);
        persistenceData.setGetDate(currentDate);
        persistenceData.setGetUid(ShiroUtils.getUserId());
        persistenceData.setPriceType(1);
        persistenceData.setProCode(order.getProCode());
        persistenceData.setBuyType("外协件");
        if(persistenceData.getAssistType().equals("整体外协")){
          persistenceData.setDataType(3);
        }else if(persistenceData.getAssistType().equals("带料外协")){
          persistenceData.setDataType(4);
        }else if(persistenceData.getAssistType().equals("锻件毛坯")){
          persistenceData.setDataType(5);
        }else if(persistenceData.getAssistType().equals("铸件毛坯")){
          persistenceData.setDataType(6);
        }
        BusBudgetAccountingCost setCost = null;
        if(persistenceData.getAssistType().equals("整体外协") || persistenceData.getAssistType().equals("铸件毛坯") || persistenceData.getAssistType().equals("锻件毛坯")){
          BusBudgetAccountingCost getBusBudgetAccountingCostCondition = new BusBudgetAccountingCost();
          getBusBudgetAccountingCostCondition.setBatId(param.getBatId());
          getBusBudgetAccountingCostCondition.setMajor(persistenceData.getMajor());
          getBusBudgetAccountingCostCondition.setPart(persistenceData.getPart());
          getBusBudgetAccountingCostCondition.setCode(persistenceData.getMajor() + "-" + persistenceData.getPart() + "-外协费-整体外协");
          setCost =accountingInfoDao.getBusBudgetAccountingCostByCondition(getBusBudgetAccountingCostCondition);
        }else if(persistenceData.getAssistType().equals("带料外协")){
          BusBudgetAccountingCost getBusBudgetAccountingCostCondition = new BusBudgetAccountingCost();
          getBusBudgetAccountingCostCondition.setBatId(param.getBatId());
          getBusBudgetAccountingCostCondition.setMajor(persistenceData.getMajor());
          getBusBudgetAccountingCostCondition.setPart(persistenceData.getPart());
          getBusBudgetAccountingCostCondition.setCode(persistenceData.getMajor() + "-" + persistenceData.getPart() + "-外协费-带料外协");
          setCost =accountingInfoDao.getBusBudgetAccountingCostByCondition(getBusBudgetAccountingCostCondition);
        }
        persistenceData.setCostId(setCost.getId());
        BusMaterialData data = new BusMaterialData();
        BeanUtil.copy(persistenceData, data);
        accountingInfoDao.insertBusMaterialData(data);
        setCost.setDataSource("1");
        accountingInfoDao.updateBusBudgetAccountingCostDataSourceByPrimaryKey(setCost);
      }
      callBack.onSuccess();
    }
  }

  /**
   * 异常处理
   *
   * @param exception 异常信息
   * @param context
   */
  @Override
  public void onException(Exception exception, AnalysisContext context) {}
}
